<?php

/**
 * Customer form.
 *
 * @package    smp-crm
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class CustomerForm extends BaseCustomerForm {
    /**
     * @see sfGuardUserForm
     */

    /**
     * Phonenumbers for deletion
     * @var array
     */
    protected $phonenumbersForDeletion = array();

    public function configure() {
        parent::configure();
        unset($this['created_at'], $this['updated_at'],
                $this['type'], $this['groups_list'],
                $this['permissions_list'], $this['last_login'],
                $this['is_super_admin'], $this['salt'], $this['algorithm']);

        $this->widgetSchema['password'] = new sfWidgetFormInputPassword();
        $this->validatorSchema['password'] = new sfValidatorString(array('max_length' => 128, 'required' => false));

        if ($this->isNew) {
            $this->validatorSchema['username'] = new sfValidatorAnd(array(
                        new sfValidatorDoctrineUnique(array('model' => $this->getModelName(), 'column' => 'username')),
                        new sfValidatorString(array('max_length' => 128))
                    ));

            $this->validatorSchema['email_address'] = new sfValidatorAnd(array(
                        new sfValidatorDoctrineUnique(array('model' => $this->getModelName(), 'column' => 'email_address')),
                        new sfValidatorString(array('max_length' => 255))
                    ));
        }

        $this->widgetSchema['title'] = new sfWidgetFormChoice(array('choices' => Doctrine::getTable('sfGuardUser')->getListTitle(), 'expanded' => false, 'multiple' => false));
        $this->validatorSchema['title'] = new sfValidatorChoice(array('choices' => array_keys(Doctrine::getTable('sfGuardUser')->getListTitle()), 'multiple' => false, 'required' => true));


        $this->widgetSchema['date_birthday'] = new sfWidgetFormJQueryDate(array('image' => '/images/calendar.png'));

        /* $lists_phone = $this->getObject()->getPhonenumbers();

          foreach ($lists_phone as $key => $phone) {
          $this->embedForm('phone_' . ($key + 1), new PhonenumberForm($phone));
          } */
        $newPhone = new PhonenumberForm();
        $this->embedForm('phone_new', $newPhone);
        $this->embedRelation('Phonenumbers');
    }

    protected function doBind(array $values) {
        if ('' === trim($values['phone_new']['name']) && '' === trim($values['phone_new']['phonenumber'])) {
            unset($values['phone_new'], $this['phone_new']);
        }

        if (isset($values['Phonenumbers'])) {
            foreach ($values['Phonenumbers'] as $i => $phone) {
                if (isset($phone['delete']) && $phone['id']) {
                    $this->phonenumbersForDeletion[$i] = $phone['id'];
                }
            }
        }

        parent::doBind($values);
    }

    protected function doUpdateObject($values) {

        foreach ($this->phonenumbersForDeletion as $index => $id) {
            unset($values['Phonenumbers'][$index]);
            unset($this->object['Phonenumbers'][$index]);
            Doctrine::getTable('Phonenumber')->findOneById($id)->delete();
        }
        $this->getObject()->fromArray($values);
    }

    public function saveEmbeddedForms($con = null, $forms = null) {
        if (null === $con) {
            $con = $this->getConnection();
        }

        if (null === $forms) {
            $forms = $this->embeddedForms;
        }

        foreach ($forms as $key => $form) {
            if ($form instanceof sfFormObject) {
                if (!in_array($form->getObject()->getId(), $this->phonenumbersForDeletion)) {
                    $form->saveEmbeddedForms($con);
                    $form->getObject()->setUserId($this->getObject()->getId());
                    $form->getObject()->save($con);
                }
            } else {
                $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
            }
        }
    }

}
